using System;
using System.Collections.Generic;
using SqlSugar;

namespace SqlSugarEx.LambdaExtend
{
	public class Demo
	{
		public static SqlSugarClient GetDb()
		{
			List<SqlFuncExternal> list = new List<SqlFuncExternal>();
			list.Add(new SqlFuncExternal
			{
				UniqueMethodName = "MyToString",
				MethodValue = delegate(MethodCallExpressionModel expInfo, DbType dbType, ExpressionContext expContext)
				{
					if (dbType != DbType.SqlServer)
					{
						throw new Exception("未实现");
					}
					return $"CAST({expInfo.Args[0].MemberName} AS VARCHAR(MAX))";
				}
			});
			ConnectionConfig config = new ConnectionConfig
			{
				ConnectionString = "Server =.; Database =Student; uid =sa; pwd =123; Connect Timeout = 500",
				DbType = DbType.SqlServer,
				IsAutoCloseConnection = true,
				ConfigureExternalServices = new ConfigureExternalServices
				{
					SqlFuncServices = list
				}
			};
			return new SqlSugarClient(config);
		}

		public static string MyToString<T>(T str)
		{
			throw new NotSupportedException("Can only be used in expressions");
		}

		public static void Init()
		{
			SqlSugarClient db = Demo.GetDb();
			List<StudentInfo> list = (from it in db.Queryable<StudentInfo>()
				where Demo.MyToString(it.ID) == "1"
				select it).ToList();
			KeyValuePair<string, List<SugarParameter>> keyValuePair = (from it in db.Queryable<StudentInfo>()
				where Demo.MyToString(it.ID) == "2"
				select it).ToSql();
			Console.WriteLine(keyValuePair);
		}
	}
}
